import { axurl } from '@/shared/axurl';
import { axhttp } from '@/shared/axhttp';
import { axconst } from '@/shared/axconst';

const url = axurl.comm;
const Model = {
  namespace: 'models_common',
  /** State 表示 Model 的状态数据 */
  state: {
    areaData: axconst.initialList,
    orgData: axconst.initialList,
    userOrgData: axconst.initialList,
    campusData: axconst.initialList,
    userCampusData: axconst.initialList,
    userData: axconst.initialList,
    roleData: axconst.initialList,
    schoolData: axconst.initialList,
    feeItemData: axconst.initialList,
    orgCerData: axconst.initialList,
    orgCerDetailData: axconst.initialList,
    payOrderData: axconst.initialList,
  },
  /** 接收action，同步更新state（同步处理Action）
   * reducers 聚合积累的结果是当前 model 的 state 对象
   * 用户行为会改变数据的时候可以通过 dispatch 发起一个 action
   * 同步行为会直接通过 Reducers 改变 State
   * 如果是异步行为（副作用）会先触发 Effects 然后流向 Reducers 最终改变 State
   */
  reducers: {
    saveArea(state, action) {
      return { ...state, areaData: action.payload };
    },
    saveSchool(state, action) {
      return { ...state, schoolData: action.payload };
    },
    saveOrg(state, action) {
      return { ...state, orgData: action.payload };
    },
    saveUserOrg(state, action) {
      return { ...state, userOrgData: action.payload };
    },
    saveCampus(state, action) {
      return { ...state, campusData: action.payload };
    },
    saveUserCampus(state, action) {
      return { ...state, userCampusData: action.payload };
    },
    saveUser(state, action) {
      return { ...state, userData: action.payload };
    },
    saveRole(state, action) {
      return { ...state, roleData: action.payload };
    },
    saveFeeItem(state, action) {
      return { ...state, feeItemData: action.payload };
    },
    saveOrgCer(state, action) {
      return { ...state, orgCerData: action.payload };
    },
    saveOrgCerDetail(state, action) {
      return { ...state, orgCerDetailData: action.payload };
    },
    savePayOrder(state, action) {
      return { ...state, payOrderData: action.payload };
    },
  },
  /** 异步处理Action */
  effects: {
    /** 查询区域 */
    *readArea({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormTree, url.readArea, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'saveArea',
          payload: response,
        });
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 查询院校 */
    *readSchool({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormData, axurl.mall.comm.school, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'saveSchool',
          payload: response,
        });
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 查询机构 */
    *readOrg({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormData, url.orgList, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'saveOrg',
          payload: response,
        });
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 查询当前用户管理的机构 */
    *readUserOrg({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormData, url.userOrgList, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'saveUserOrg',
          payload: response,
        });
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 根据机构Id查询校区 */
    *readCampus({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormData, url.campus, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'saveCampus',
          payload: response,
        });
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 根据当前机构id获取用户权限的校区 */
    *readUserCampus({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormData, url.userCampus, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'saveUserCampus',
          payload: response,
        });
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 查询用户 */
    *readUser({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormData, url.user, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'saveUser',
          payload: response,
        });
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 查询角色 */
    *readRole({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormData, url.role, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'saveRole',
          payload: response,
        });
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 获取学费或证书费的收费项
     *  @param itemSort 1 - 学费  3 - 证书费
     */
    *readFeeItem({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormData, url.getFeeItemList, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'saveFeeItem',
          payload: response,
        });
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 机构证书待选列表（主要用于下拉列表选择）
     *  @param campusId
     */
    *readOrgCer({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormList, url.orgCerList, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'saveOrgCer',
          payload: response,
        });
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 机构证书明细待选列表（主要用于下拉列表选择）
     *  @param id 机构证书id
     */
    *readOrgCerDetail({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormData, url.orgCerDetails, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'saveOrgCerDetail',
          payload: response,
        });
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 查询缴费人 */
    *readPayer({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.getFormList, url.customer, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        yield put({
          type: 'savePayer',
          payload: response,
        });
        if (callback) callback();
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 创建缴费人 */
    *createPayer({ params, payload, callback }, { call, put }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.postForm, url.addCustomer, params, payload);
      if (response && response.success) {
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 根据订单号返回订单是否支付 */
    *checkPay({ params, payload, callback }, { call }) {
      const response = yield call(axhttp.get, url.checkPay, params, payload);
      if (response && response.success) {
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 扫码支付和付款码支付 */
    *createPay({ params, payload, callback }, { call }) {
      const response = yield call(axhttp.postForm, url.createPay, params, payload);
      if (response && response.success) {
        if (callback) callback(response);
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
    /** 获取打印模板 */
    *readTemplate({ params, payload, callback }, { call }) {
      /** call执行异步函数 */
      const response = yield call(axhttp.get, axurl.resource.readTemplate, params, payload);
      if (response && response.success) {
        /** put发出一个 Action，类似于 dispatch */
        localStorage.setItem('ax-data-template', JSON.stringify(response.data));
        if (callback) callback();
      } else return Promise.reject(response);
      return Promise.resolve(response);
    },
  },
};
export default Model;
